package com.quan.server.beans;

import javax.persistence.Entity;
import javax.persistence.Id;

/**
 * 订单年月日
 */
@Entity
public class Income {
    @Id
    public String id = "";
    public String customerId = "";
    public double moneyDay = 0;//当天收入
    public double moneyMonth = 0;//当月收入
    public double moneyTotal = 0;//总收入
    public double moneyRest = 0;//总余额
    public double moneyFreeze = 0;//冻结收入, 已经申请提现的money数目，提现成功之后，总余额要减去freeze
    public int lastDay = 0;//最后记录的日期 天
    public int lastMonth = 0;//最后记录的日期 月
    public int lastYear = 0;//最后记录的日期 年

    public static Income updateIncome(Income income, int currentYear, int currentMonth, int currentDay, double money) {
        int lastYear = income.lastYear;
        int lastMonth = income.lastMonth;
        int lastDay = income.lastDay;
        if (currentYear > lastYear) {
            income.lastYear = currentYear;
            income.lastMonth = currentMonth;
            income.lastDay = currentDay;
            //当月，当日收入 清零
            income.moneyMonth = 0;
            income.moneyDay = 0;
        } else if (currentYear == lastYear && currentMonth > lastMonth) {
            income.lastMonth = currentMonth;
            income.lastDay = currentDay;
            income.moneyMonth = 0;
            income.moneyDay = 0;
        } else if (currentYear == lastYear && currentMonth == lastMonth && currentDay > lastDay) {
            income.lastDay = currentDay;
            income.moneyDay = 0;
        }
        income.moneyDay = income.moneyDay + money;
        income.moneyMonth = income.moneyMonth + money;
        income.moneyTotal = income.moneyTotal + money;
        income.moneyRest = income.moneyRest + money;
        return income;
    }

    /**
     * 申请提现
     *
     * @param income
     * @param withdrawMoney 申请提现的money
     * @return
     */
    public static Income requireWithdrawMoney(Income income, double withdrawMoney) {
        double enableWithdrawMoney = getEnableWithdrawMoney(income);
        if (enableWithdrawMoney > withdrawMoney) {
            income.moneyFreeze = income.moneyFreeze + withdrawMoney;
        }
        return income;
    }

    /**
     * 获取可以提现的money
     *
     * @param income
     * @return
     */
    public static double getEnableWithdrawMoney(Income income) {
        double moneyRest = income.moneyRest;
        double moneyFreeze = income.moneyFreeze;//当前已经冻结的money
        double enableWithdrawMoney = moneyRest - moneyFreeze;
        return enableWithdrawMoney;
    }

    public static Income finishWithdrawMoney(Income income) {
        income.moneyRest = income.moneyRest - income.moneyFreeze;
        income.moneyFreeze = 0;
        return income;
    }
}
